package org.fu.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import org.fu.crowd.entity.Common;
import org.fu.crowd.entity.Role;
import org.fu.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;


    /**
     * 获取分页数据
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @PreAuthorize("hasRole('部长')")//在WebAppSecurityConfig加上@EnableGlobalMethodSecurity(prePostEnabled = true)
    @ResponseBody
    @RequestMapping("/role/getPageInfo")
    public Common<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
            @RequestParam(value = "keyword",defaultValue = "")String keyword){
        //获取分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
        if (pageNum>pageInfo.getPages()){
            pageInfo = roleService.getPageInfo(pageInfo.getPages(), pageSize, keyword);
        }
        return Common.successWithoutData(pageInfo);
    }




    /**
     * 保存添加的role
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("/role/save")
    public Common<String> saveRole(Role role){
        roleService.saveRole(role);
        return Common.successWithoutData();
    }





    /**
     * 修改role
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("/role/update")
    public Common<String> updateRole(Role role){
        roleService.updateRole(role);
        return Common.successWithoutData();
    }





    /**
     * 删除单个或批量删除
     */
    @ResponseBody
    @RequestMapping("/role/delete")
    public Common<String> removeRoles(@RequestBody List<Integer> list){
        roleService.removeRole(list);
        return Common.successWithoutData();
    }


}
